package com.tony.blog.server.controller.web;

import com.tony.blog.api.response.BaseResponse;
import com.tony.blog.api.response.StatusCode;
import com.tony.blog.api.utils.Constant;
import com.tony.blog.api.utils.ValidatorUtil;
import com.tony.blog.model.entity.SysUserEntity;
import com.tony.blog.model.request.IdRequest;
import com.tony.blog.model.request.UserRequest;
import com.tony.blog.server.controller.AbstractController;
import com.tony.blog.server.service.web.IndexUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: IndexuserController
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/18/20:52
 * @Description: 前端用户
 */
@RestController
@RequestMapping("web/user")
public class IndexUserController extends AbstractController {

    private static final Logger log = LoggerFactory.getLogger(IndexUserController.class);

    @Autowired
    private IndexUserService indexUserService;

    /**
    * @Description: register 用户注册
    * @Param: [userRequest, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/18 21:01
    */
    @RequestMapping("register")
    public BaseResponse register(@Validated @RequestBody UserRequest userRequest, BindingResult result) {
        if (StringUtils.isBlank(userRequest.getUsername())) {
            return new BaseResponse(StatusCode.UserNameCanNotBlank);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNoneBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidCode.getCode(),checkRes);
        }
        //校验用户名规范(正则表达式判断，由字母、数字组成)
        if (!userRequest.getUsername().matches(Constant.RegexRoute)) {
            return new BaseResponse(StatusCode.UserNameRegisterInvalid);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            userRequest.setType(0); //0-用户 1-管理员
            userRequest.setStatus(1);
            log.info("前端用户注册所提交的信息为：{}",userRequest);
            log.info("前端用户注册的结果为：{}",indexUserService.registerUser(userRequest));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
    * @Description: register 用户消息中心
    * @Param: []
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/19 20:45
    */
    @RequestMapping(value = "msg",method = RequestMethod.GET)
    public BaseResponse register() {
        SysUserEntity entity = getUser();
        if (null == entity) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexUserService.getUserMsg(entity.getUserId().intValue()));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: uploadMyImage 上传用户头像
    * @Param: [request]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/20 22:57
    */
    @RequestMapping(value = "image/upload",method = RequestMethod.POST)
    public BaseResponse uploadMyImage(MultipartHttpServletRequest request){
        if (null==getUser()){
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        BaseResponse response=new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexUserService.uploadImg(request,getUserId()));
        }catch (Exception e){
            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

    /**
    * @Description: readMsg 读消息
    * @Param: [request, result]
    * @return: com.tony.blog.api.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/9/26 21:30
    */
    @PostMapping("msg/read")
    public BaseResponse readMsg(@RequestBody @Validated IdRequest request,BindingResult result) {
        SysUserEntity entity = getUser();
        if (entity == null) {
            return new BaseResponse(StatusCode.UserNotLogin);
        }
        String checkRes = ValidatorUtil.checkResult(result);
        if (StringUtils.isNotBlank(checkRes)) {
            return new BaseResponse(StatusCode.InvalidParams.getCode(),checkRes);
        }
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            response.setData(indexUserService.readMsg(request,entity.getUserId()));
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }




























}
